package com.sx.ems.controller.sys;

import com.sx.ems.entity.SysDept;
import com.sx.ems.service.SysDeptService;
import com.sx.ems.utils.MsgBean;
import com.sx.ems.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @RequestMapping("/list")
    @RequiresPermissions("sys:dept:list")
    public MsgBean list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysDeptService.queryMaterialPage(params);
        return MsgBean.ok().put("page", page);
    }

    @RequestMapping("/select")
    @RequiresPermissions("sys:dept:select")
    public MsgBean select() {
        List<SysDept> list = sysDeptService.list(null);
        return MsgBean.ok().put("list", list);
    }

    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dept:info")
    public MsgBean info(@PathVariable("id") Long id) {
        SysDept dept = sysDeptService.getById(id);
        return MsgBean.ok().put("dept", dept);
    }

    @RequestMapping("/save")
    @RequiresPermissions("sys:dept:save")
    public MsgBean save(@RequestBody SysDept dept) {
        sysDeptService.save(dept);
        return MsgBean.ok();
    }

    @RequestMapping("/update")
    @RequiresPermissions("sys:dept:update")
    public MsgBean update(@RequestBody SysDept material) {
        sysDeptService.updateById(material);
        return MsgBean.ok();
    }

    @RequestMapping("/delete")
    @RequiresPermissions("sys:dept:delete")
    public MsgBean delete(@RequestBody Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        list.forEach(z->{
            sysDeptService.delete(z);
        });
        return MsgBean.ok();
    }
}
